package com.lebang.service;

import com.alibaba.fastjson.JSON;
import com.lebang.domain.AppEatConfig;
import com.lebang.domain.AppEatMemberRecord;
import com.lebang.entity.RequestHeaders;
import com.lebang.example.AppEatConfigExample;
import com.lebang.example.AppEatMemberRecordExample;
import com.lebang.mapper.AppEatMemberRecordMapper;
import com.lebang.model.Whether;
import com.lebang.model.enums.buss.TaskCategory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Iterator;
import java.util.List;

/**
 * 吃饭赚
 *
 * @Author hwq
 * @Date 2020-01-08
 */
@Service
public class AppEatConfigService extends BaseService<AppEatConfig, Integer, AppEatConfigExample> {
    @Autowired
    private AppEatMemberRecordMapper recordMapperr;
    private static final String BASE_KEY = "AppEatConfig";
    @Autowired
    private RedisCacheService redisCacheService;
    @Autowired
    private PointService pointService;
    @Autowired
    private MemberNoticeService memberNoticeService;


    public void delCache() {
        redisCacheService.delCache(BASE_KEY);
    }



    /**
     * 添加这个时间段的待领取记录
     *
     * @param memberId
     * @param id
     * @return
     */
    public int addRecord(String memberId, Integer id) {
        AppEatConfig config = this.selectById(id);
        AppEatMemberRecord record = new AppEatMemberRecord();
        record.setCreateTime(LocalDateTime.now());
        record.setModifyTime(LocalDateTime.now());
        record.setHasDeleted(Whether.NO.getDbIndex());
        record.setMemberId(memberId);
        record.setEatId(id);
        record.setEvent(config.getTitle());
        record.setStatus(0);
        record.setPoint(config.getPoint());
        record.setCreatDate(LocalDate.now());
         recordMapperr.insertSelective(record);
        return record.getPoint();
    }

    public void reward(String memberId, RequestHeaders header) {
        AppEatMemberRecordExample example = new AppEatMemberRecordExample();
        example.createCriteria().andMemberIdEqualTo(memberId).andStatusEqualTo(0);
        List<AppEatMemberRecord> list = recordMapperr.selectByExample(example);
        list.forEach(s -> {
            pointService.addPoint(memberId, s.getPoint(), TaskCategory.EAT_EARNS, header,"");
            s.setStatus(1);
            s.setModifyTime(LocalDateTime.now());
            recordMapperr.updateByPrimaryKeySelective(s);
            memberNoticeService.addNoticeAndPush(TaskCategory.EAT_EARNS.getTitle(), s.getEvent(), s.getMemberId(), header);
        });
    }
}
